package cn.hbads.support.compare.supoort;

import org.javers.core.diff.changetype.PropertyChangeMetadata;
import org.javers.core.diff.changetype.ValueChange;
import org.javers.core.diff.custom.CustomPropertyComparator;
import org.javers.core.metamodel.property.Property;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.Optional;

/**
 * <b> StringPropertyComparator </b>
 *
 * @author : yvvb
 * @date : 2025/4/20
 */
public class StringPropertyComparator implements CustomPropertyComparator<String, ValueChange> {

    @Override
    public Optional<ValueChange> compare(String left, String right, PropertyChangeMetadata metadata, Property property) {
        if (equals(left, right)) {
            return Optional.empty();
        } else {
            return Optional.of(new ValueChange(metadata, left, right));
        }
    }

    @Override
    public boolean equals(String a, String b) {
        if (!StringUtils.hasLength(a) && !StringUtils.hasLength(b)) {
            return true;
        } else if (Objects.nonNull(a)) {
            return a.equals(b);
        } else {
            return false;
        }
    }

    @Override
    public String toString(String value) {
        return value;
    }
}
